package com.zjut.multithreading.pojo;

import org.bson.Document;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Ping implements Callable<Document> {
    private String ipAddress;

    public Ping(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    @Override
    public Document call() throws Exception {
        String pingCommand = "";
        if (System.getProperty("os.name").startsWith("Windows")) {
            // For Windows
            pingCommand = "ping " + ipAddress;
        } else {
            // For Linux and macOS
            pingCommand = "ping " + ipAddress;
        }

        // Execute ping command
        Process process = Runtime.getRuntime().exec(pingCommand);

        // Read the ping output
        BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
        StringBuilder result = new StringBuilder();
        String line;
        while ((line = inputStream.readLine()) != null) {
            result.append(line).append("\n");
        }
        inputStream.close();

        // 处理ip回显信息
        Document doc =new Document("deviceId",1).append("deviceName", "测试设备").append("devicegroupName", "设备").append("ipAddress", ipAddress)
                .append("checkDate", "").append("checkTime", "").append("checkHour", "");
        String content[] =  result.toString().split("\n");
        if(result.toString().contains("请求找不到主机")){
            doc.append("status", 0)
                    .append("send", 0)
                    .append("receive", 0)
                    .append("lost",0)
                    .append("lostRate",0)
                    .append("minDelay", 0)
                    .append("maxDelay", 0)
                    .append("avgDelay", 0);
        }
        else if (content.length == 9){
            List<Integer> data = new ArrayList<>();
            Pattern pattern = Pattern.compile("\\d+");
            Matcher matcher = pattern.matcher(content[8].trim());
            while (matcher.find()) {
                data.add(Integer.parseInt(matcher.group()));
            }
            doc.append("status", 0)
                    .append("send", data.get(0))
                    .append("receive", data.get(1))
                    .append("lost",data.get(2))
                    .append("lostRate",data.get(3))
                    .append("minDelay", 0)
                    .append("maxDelay", 0)
                    .append("avgDelay", 0);
        }
        else{
            List<Integer> data = new ArrayList<>();
            Pattern pattern = Pattern.compile("\\d+");
            Matcher matcher = pattern.matcher(content[8].trim() + content[10].trim());
            while (matcher.find()) {
                data.add(Integer.parseInt(matcher.group()));
            }
            doc.append("status", 1)
                    .append("send", data.get(0))
                    .append("receive", data.get(1))
                    .append("lost",data.get(2))
                    .append("lostRate",data.get(3))
                    .append("minDelay", data.get(4))
                    .append("maxDelay", data.get(5))
                    .append("avgDelay", data.get(6));
        }

        return doc;
    }
}
